#include "camcontrol.h"
#include "src/db/DbModule.h"
#include <QFileDialog>


CamControl::CamControl(QWidget *parent)
    : QWidget(parent)
{
	ui.setupUi(this);
	ui.typeCombo->addItem("LevelOne");
	ui.typeCombo->addItem("Firewire");
	ui.previewSelect->addItem("source1");
	ui.previewSelect->addItem("source2");
	ui.previewSelect->addItem("source3");
	ui.previewSelect->addItem("source4");
	ui.previewSelect->addItem("preview");
	tcpSocket = new QTcpSocket(this);
	connect( ui.dirSelect, SIGNAL( clicked() ) , this, SLOT( selectDirectory() ) );
	connect(ui.addButton,SIGNAL (clicked()), this, SLOT (addCam()));
	connect(ui.saveButton,SIGNAL (clicked()), this, SLOT (saveModel()));
	connect(ui.loadButton,SIGNAL (clicked()), this, SLOT (load()));
	connect(ui.previewButton,SIGNAL (clicked()), this, SLOT (select()));
	connect(ui.listView,SIGNAL (clicked(QModelIndex)), this, SLOT (show(QModelIndex)));
}

CamControl::~CamControl()
{

}
void CamControl::show(QModelIndex index) {

	CamData *dat = list->cams->get(index.row());
	current = dat;
	printf(dat->dir->get().data());
	printf(" clicked\n");
	std::ostringstream os;
	os << dat->dir->get() << "cam.counter";
	std::string counterName = os.str();
	QFile file(counterName.data());
	if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
	     return;
	if (!file.atEnd()) {
	    QByteArray line = file.readLine();
	      QString content = QString(line);
	      int num = content.toInt()-1;
	      printf("num %d\n",num);
	      std::ostringstream thumbFile;
	      thumbFile << dat->dir->get() << "cam-" << num << ".jpg";
	      printf(thumbFile.str().data());
	      QImage img = QImage(QString(thumbFile.str().data()));
	      QGraphicsScene *sc = new QGraphicsScene();
	      ui.graphicsView->setScene(sc);
	      ui.graphicsView->scene()->setSceneRect(0, 0, 160, 120);
	      ui.graphicsView->scene()->setBackgroundBrush(QBrush::QBrush(img.scaled(160,120))) ;
//	      ui.graphicsView->setBackgroundBrush(img);
	}
}

void CamControl::setPool(DataPool *_pool){
	pool=_pool;
	Model *m = pool->getRootModel("camList");
	if (m==0) {
		m= new CamList(pool,"camList");
		pool->addRootModel(m);
	}
	display = (VideoDisplay*) pool->getRootModel("display");
	list = (CamList*)m;
	propList = new NPropList(list->cams,this);
	ui.listView->setModel(propList);
}

void CamControl::addCam() {
	CamData *data = new CamData(pool,ui.nameEdit->text().toUtf8().data());
	data->index->set(list->cams->size());
	data->ip->set(ui.ipEdit->text().toUtf8().data());
	data->dir->set(ui.dirEdit->text().toUtf8().data());
	printf("size: %d\n",data->index->get());
	printf(ui.nameEdit->text().toUtf8().data());
	printf(ui.ipEdit->text().toUtf8().data());
	printf(ui.dirEdit->text().toUtf8().data());
	printf("\n");
	list->cams->append(data);
	ui.listView->reset();
	
	tcpSocket->abort();
	tcpSocket->connectToHost("127.0.0.1",6666);
	tcpSocket->write("MJPEG\n");
	tcpSocket->write(ui.ipEdit->text().toUtf8().data());
	tcpSocket->write("\n");
	tcpSocket->write(ui.dirEdit->text().toUtf8().data());
	tcpSocket->write("cam\n");
}

void CamControl::load() {
	Db *db = (Db*)pool->getIdProvider();
	CamList *data = (CamList*)db->loadSavePoint("camcontrol",ui.loadCombo->currentText().toUtf8().data());
	if (data==0)
		return;
	propList = new NPropList(data->cams,this);
	ui.listView->setModel(propList);
}

void CamControl::saveModel() {
	
	Db *db = (Db*)pool->getIdProvider();
	db->saveModel(list,ui.saveEdit->text().toUtf8().data(),"camcontrol");
	std::list<std::string> l = db->loadSavePoints("camcontrol");
		std::list<std::string>::const_iterator it;
		for (it=l.begin();it!=l.end();++it) {
			std::string val = (std::string)*it;
			ui.loadCombo->addItem(QString(val.data()));
		}
		db->loadInstance(list->getId());
}

Texture* CamControl::getSelectedTexture() {
	return ((Prop<DisplayPanel*>*)(display->getProp(ui.previewSelect->currentText().toLatin1().data())))->get()->text->get();
}

void CamControl::select() {
	Texture *t = getSelectedTexture();
	std::ostringstream sst;
	sst << current->dir->get();
	t->filebase->set(sst.str());
	t->current->set(-1);
	t->playMode->set(2);
}

void CamControl::selectDirectory() {
	QFileDialog dialog(this);
	 dialog.setFileMode(QFileDialog::DirectoryOnly);
	 
	 if (dialog.exec()) {
		 
		QStringList files = dialog.selectedFiles();
		if (files.size()==1) {
			ui.dirEdit->setText(files.first());
		}
		 
	 }
}
